// pages/me/verificationCode/verificationCode.js
const app=getApp();
var util = require('../../../utils/util.js');
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true,
    focus:false,
    inputCodeValue: '',
    num:60,
    telephone:'',
    showCancel:false,
    isIf:true,
    color:'#999999',
    isRegister: 0,//1=找回密码  2=更换手机号  0=注册
    codeBtnText:'下一步',
    flag: 1  //2=找回密码  4=更换手机号  1=注册
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that=this;
    that.setData({
      telephone: options.telephone,
      isRegister:options.isRegister
    })
    if (options.isRegister==2){
        that.setData({
          codeBtnText:'完成',
          flag:4
        })
    } else if (options.isRegister == 1){
      that.setData({
        codeBtnText: '下一步',
        flag: 2
      })
    }
    else{
      that.setData({
        codeBtnText: '下一步',
        flag: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var numVar=60;
    timer=setInterval(function(){
      if (numVar <= 0) {
        numVar=0;
        clearInterval(timer);
        that.setData({
          isIf: false,
          num: numVar,
          color: '#3486ff',
        })
      }
      numVar--;
      that.setData({
        num: numVar
      })

    },1000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindButtonTap: function () {
    this.setData({
      focus: true,
    })
  },
  inputPhoneNum: function (e) {
    this.setData({
      disabled: false,
      inputCodeValue: e.detail.value,
      showCancel:true
    })
  },
  bindCancelbtn:function(){
    this.setData({
      inputCodeValue:'',
      showCancel: false
    })
  },
  // 重新获取验证码
  bindRegain:function(){
    var that=this;
    if (!that.data.isIf){
      wx.request({
        url: 'https://papi.zcyun.cn/v1/validCode/send',
        data: {
          value: that.data.telephone,
          sendMode: 2,
          flag: that.data.flag,
          platformId: app.globalData.platformId
        },
        header: {},
        method: 'POST',
        success: function (res) {
          clearInterval(timer);
          console.log(res.data);
          if (res.data.code != 0) {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }else{
            that.setData({
              isIf:true,
              num:60,
              color:'#999999',
            })
            var numVar = 60;
            timer = setInterval(function () {
              if (numVar <= 0) {
                numVar = 0;
                clearInterval(timer);
                that.setData({
                  isIf: false,
                  num: numVar,
                  color: '#3486ff',
                })
              }
              numVar--;
              that.setData({
                num: numVar
              })
            }, 1000)
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  // 验证码校验
  bindNextBtn:function(){
    var that=this;
   wx.request({
     url: 'https://papi.zcyun.cn/v1/validCode/validate',
     data: {
       flag: that.data.flag,
       platformId: app.globalData.platformId,
       sendMode: 2,
       value: that.data.telephone,
       valCode: that.data.inputCodeValue
     },
     header: {
       'content-type': 'application/json' // 默认值
     },
     method: 'POST',
     success: function (res) {
       console.log(res.data);
       if (res.data.code == 0) {
         if (that.data.isRegister==0){
           wx.navigateTo({
             url: '../completeRegister/completeRegister?token=' + res.data.token + '&telephone=' + that.data.telephone + '&isRegister=' + that.data.isRegister,
           })
         } else if (that.data.isRegister == 2) {
           console.log(that.data.inputCodeValue);
           var myDate = new Date();
           var timestamp = new Date().getTime();
           var param = {
             'telephone': that.data.telephone,
             'valcode': that.data.inputCodeValue
           }
           var params = JSON.stringify(param);
           wx.request({
             url: 'https://papi.zcyun.cn/v1/user/telephone',
             data: {
               'telephone': that.data.telephone,
               'valcode': that.data.inputCodeValue
             },
             method: 'PUT',
             header: {
               'content-type': 'application/json', // 默认值
               'openId': app.globalData.openId,
               'ts': timestamp,
               'sign': util.sha1("PUT" + '/v1/user/telephone' + params + timestamp + app.globalData.openKey)
             },
             success: function (res) {
               console.log(res.data);
               if (res.data.code == 0) {
                 wx.redirectTo({
                   url: '../pages/usersinfo/usersinfo'
                 })
               }
             }
           })
         }
         else{
           clearInterval(timer);

           wx.navigateTo({
             url: '../findPwd/findPwd?token=' + res.data.token + '&telephone=' + that.data.telephone,
           })
         }
       } else {
         wx.showToast({
           title: res.data.msg,
           icon: "none",
           duration: 2000
         })
       }

     },
     fail: function (res) { },
     complete: function (res) { },
   })
  }

})